package abd.pr1.Controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;

import javax.sql.DataSource;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;

import abd.pr1.Interface.AdminUserPanel;
import abd.pr1.Mappers.UsuariosMapper;
import abd.pr1.Models.Usuario;

public class AdminUserController implements ActionListener {
	
	private UserPanelController userPanelController;
	private AdminUserPanel adminUserPanel;
	private DataSource ds;
	private Usuario user;
	
	public AdminUserController(UserPanelController userPanelController,AdminUserPanel adminUserPanel,DataSource ds, Usuario user){
		
		this.adminUserPanel= adminUserPanel;
		this.ds=ds;
		this.user=user;
		this.userPanelController = userPanelController;
		
		this.adminUserPanel.addListener(this);
		
		refreshUserData();

	}

	
	public void refreshUserData(){
		this.adminUserPanel.setNick(user.getNick());
		this.adminUserPanel.setEdad(user.getNacimiento());
		this.adminUserPanel.setImagen(new ImageIcon(user.getImagen()));
		this.adminUserPanel.setPassword(user.getPassword());
		
	}
	
	

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("seleccion_avatar")){
			
			JFileChooser fc = this.adminUserPanel.getFileChooser();
			FileNameExtensionFilter filtroImagen=new FileNameExtensionFilter("JPG|PNG|GIF","jpg","png","gif");
			fc.setAcceptAllFileFilterUsed(false);
			fc.setFileFilter(filtroImagen);
			
			int returnVal = fc.showOpenDialog(this.adminUserPanel);
			
			if (returnVal == JFileChooser.APPROVE_OPTION) {
	            File file = fc.getSelectedFile();
	            byte[] b = new byte[(int) file.length()];

	            if(file.length()<65*1024){
	            	try {
		                  FileInputStream fileInputStream = new FileInputStream(file);
		                  fileInputStream.read(b);
		                  
		                  this.user.setImagen(b);
		                  this.adminUserPanel.setImagen(new ImageIcon(b));
		                  
		                  fileInputStream.close();
		                  
		             } catch (FileNotFoundException e1) {
		                         System.out.println("File Not Found.");
		                         e1.printStackTrace();
		             }
		             catch (IOException e1) {
		                      System.out.println("Error Reading The File.");
		                       e1.printStackTrace();
		             }
	            } else {
	            	JOptionPane.showMessageDialog(null, "Imagen demasiado grande. Tiene que ser menor de 64 KB", "Error en imagen", JOptionPane.ERROR_MESSAGE);
	            }
	            
	            
	        }
			
		} else if(e.getActionCommand().equals("guardar_user")){
			
			this.user.setPassword(this.adminUserPanel.getPassword());
			
			 
			Date nacimiento;
			nacimiento = this.adminUserPanel.getNacimiento();
			this.user.setNacimiento(nacimiento);
			
			new UsuariosMapper(ds).update(user);
			this.userPanelController.refreshUserData();
			this.adminUserPanel.dispose();
			
			
			
		} else if(e.getActionCommand().equals("cerrar_user")){
			adminUserPanel.dispose();
		}
	}

}
